package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import models.Favourite;
import models.Recommend;
import models.SolutionSubmission;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class ajxAddFavouriteServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			//resp.setContentType("text/xml");
			PrintWriter out = resp.getWriter();
	        UserService userService = UserServiceFactory.getUserService();
	        String userEmail = userService.getCurrentUser().getEmail().toLowerCase(); 
			
			String SolID = req.getParameter("txtsol_id");
			if ( SolID == null ){
				resp.sendRedirect("/home.jsp?error=Solution id is null.");
			}
			Long lSolID = Long.parseLong(SolID);
			
	   		//ADD NEW RATING NOM
			Objectify ofy = ObjectifyService.begin();
			Favourite FAV = new Favourite();
			FAV.setSolID(lSolID);
			FAV.setFavouriteON(new Date());
			FAV.setFavouriteOf(userEmail);
			ofy.put(FAV);
			
			//out.println("Favourite succesfully Updated!");
			resp.sendRedirect("/modules/default/display-solution.jsp?sol_id=" + SolID);
			
		} catch(Exception ee){
			ee.printStackTrace();
		}
	}
}

